<template>
  <view class="upload_img_comp">
    <u-upload
      ref="uUpload"
      :before-remove="beforeRemove"
      :action="action"
      :file-list="defaultFileList"
      :max-count="maxCount"
      :show-progress="true"
      :header="header"
      :form-data="formData"
      @on-uploaded="onSuccess"
      @on-remove="onRemove"
    ></u-upload>
  </view>
</template>

<script>
import Config from '@/config/config.conf';
export default {
  components: {},
  props: {
    fileList: {
      // 当前已经选中的图片列表
      type: Array,
      default: () => []
    },
    defaultFileList: {
      // 默认文件列表
      type: Array,
      default: () => []
    },
    maxCount: {
      // 最大数量
      type: Number,
      default: 1
    },
    channelId: {
      type: [Number, String],
      default: ''
    }
  },
  data() {
    return {
      formData: {
        channel_id: this.channelId
      },
      header: {
        token: this.$store.state.user.token
      },
      action: ''
    };
  },
  created() {
    this.action = (process.env.NODE_ENV === 'development' ? 'http://zsgh.center999.com/' : Config.domain) + 'api/common/nupload';
  },
  methods: {
    upload() {
      // 手动上传
      this.$refs.uUpload.upload();
    },
    onSuccess(lists) {
      // 上传成功
      console.log(lists)
      let resultList = []
      lists.forEach((item, index) => {
        resultList.push(item.response.code.fullurl)
      })
      this.$emit('change', resultList);
    },
    onRemove(index, lists, name){
      // 移除图片时触发
      console.log(index, lists, name)
      let resultList = this.fileList
      resultList.splice(index, 1)
      this.$emit('change', resultList);
    },
    beforeRemove() {
      // 删除图片前操作, 如果返回false，终止移除操作, 如果返回true，执行移除操作
      // return false;
    }
  }
};
</script>

<style scoped lang="scss">
.upload_img_comp {
}
</style>
